package com.zion.tencent_dnspod_admin.task;

import com.zion.tencent_dnspod_api.config.TencentConfig;
import com.zion.tencent_dnspod_api.service.DnsPodServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author ZionChen
 * @Date 2024/1/6 00:17
 * @PackageName:com.zion.dnspod_update.task
 * @ClassName: DnsTask
 * @Description: TODO
 * @Version 1.0
 */
@Component
@Slf4j(topic = "sys_info")
public class DnsTask {

    @Resource
    private DnsPodServiceImpl dnsPodService;

    /**
     * 腾讯配置
     */
    @Resource
    private TencentConfig tencentConfig;

    @Scheduled(cron = "0 */5 * * * ?")
    public void scheduledDNSUpdates(){
        String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        log.info("开始执行定时任务。当前时间={}",format);
        try{
            dnsPodService.updateDns(tencentConfig.getDomainName());
        }catch (Exception e){
            log.error("执行定时任务失败",e);
        }
        log.info("执行定时任务完毕");
    }

}
